﻿using System.Diagnostics.Contracts;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DML;
using System;
using System.Linq;
using System.Collections.Generic;
using Resn.WhuPub.DAL;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现部门部门的属性和方法
        /// </summary>
        public class Department :Entity<DepartmentModel>
        {
                #region 构造函数

                /// <summary>
                /// 创建部门实例
                /// </summary>
                /// <param name="model">部门模型</param>
                public Department(DepartmentModel model)
                        : base(model)
                {
                }

                #endregion

                #region 属性

                #region 基本属性


                /// <summary>
                /// 获取公司部门所属校区/分公司的Id
                /// </summary>
                /// <value>
                /// 公司部门所属校区/分公司的Id
                /// </value>
                public string BranchId
                {
                        get
                        {
                                return Model.BranchId;
                        }
                }

                /// <summary>
                /// 获取或设置公司部门的名称
                /// </summary>
                /// <value>
                /// 公司部门的名称
                /// </value>
                public string Name
                {
                        get
                        {
                                return Model.Name;
                        }
                        set
                        {
                                Model.Name = value;
                                this.NotifyPropertyChanged(i=>i.Name);
                        }
                }

               

                #endregion 基本属性

                

                /// <summary>
                /// 获取部门所属的校区/分公司
                /// </summary>
                /// <value>
                /// 部门所属的校区/分公司
                /// </value>
                public Branch Branch
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<BranchModel>(BranchId);
                                return new Branch(model);
                        }
                }

                /// <summary>
                /// 获取部门下属开设的专业
                /// </summary>
                /// <value>
                /// 部门下属开设的专业
                /// </value>
                public List<MajorSetup> MajorSetups
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<MajorSetupModel>().Where(i => i.DepartmentId == Id).ToList();

                                var result = new List<MajorSetup>();

                                models.ForEach(i => result.Add(new MajorSetup(i)));

                                return result;
                        }
                }

                public List<MajorOperation> MajorOperations
                {
                        get
                        {
                                var majorSetups = MajorSetups;

                                var result = new List<MajorOperation>();

                                majorSetups.ForEach(i => result.AddRange(i.MajorOperations));

                                return result;
                        }
                }

                public List<Course> Courses
                {
                        get
                        {
                                var majorOperations = MajorOperations;

                                var result = new List<Course>();

                                majorOperations.ForEach(i => result.AddRange(i.Courses));

                                return result;
                        }
                }

                /// <summary>
                /// 获取部门下属的联系人
                /// </summary>
                /// <value>
                /// 部门下属的联系人
                /// </value>
                public List<Contact> Contacts
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<ContactModel>().Where(i => i.DepartmentId == Id).ToList();

                                var contacts = new List<Contact>();

                                models.ForEach(i => contacts.Add(new Contact(i)));

                                return contacts;
                        }
                }

                #endregion 属性

                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static Department Select(string id)
                {
                        return Select<Department>(id);
                }


                /// <summary>
                /// 添加对象到数据库
                /// </summary>
                /// <exception cref="System.InvalidOperationException">
                /// 分公司下属部门名称必须为空
                /// or
                /// 校区下属部门名称不能为空
                /// or
                /// 该名称部门已存在，无法添加
                /// or
                /// 分公司下属部门已存在，无法添加
                /// </exception>
                public override void Insert()
                {
                        var corporateBranch = Branch;
                        var corporate = Branch.Corporate;
                        var departments = corporateBranch.Departments;

                        switch (corporate.CorporateLevel)
                        {
                                case CorporateLevel.公司:
                                        if (!string.IsNullOrEmpty(Name))
                                        {
                                                throw new InvalidOperationException("分公司下属部门名称必须为空");
                                        }
                                        break;

                                default:
                                        if (string.IsNullOrEmpty(Name))
                                        {
                                                throw new InvalidOperationException("校区下属部门名称不能为空");
                                        }
                                        break;
                        }

                        
                        if (DBCheck.ExistName(departments, Name))
                        {
                                throw new InvalidOperationException("该名称部门已存在，无法添加");
                        }

                        if (corporate.CorporateLevel == CorporateLevel.公司 && departments.Count > 1)
                        {
                                throw new InvalidOperationException("分公司下属部门已存在，无法添加");
                        }

                        base.Insert();
                }

                /// <summary>
                /// 删除对象从数据库
                /// </summary>
                /// <exception cref="System.InvalidOperationException">该部门下属至少1个联系人，无法删除</exception>
                public override void Delete()
                {
                        var majorSetups = MajorSetups;

                        foreach (var majorSetup in majorSetups)
                        {
                                majorSetup.Delete();
                        }

                        var contacts = Contacts;

                        foreach (var contact in contacts)
                        {
                                contact.Delete();
                        }

                        base.Delete();
                }

                /// <summary>
                /// 更新对象到数据库
                /// </summary>
                /// <exception cref="System.InvalidOperationException">该名称部门已存在</exception>
                public override void Update()
                {
                        var departments = Branch.Departments;

                        if (DBCheck.ExistName(departments, Name))
                        {
                                throw new InvalidOperationException("该名称部门已存在");
                        }

                       base.Update();
                }

                /// <summary>
                /// 创建联系人
                /// </summary>
                public Contact CreateContact(string name, string title = "", string duty = "", string telephone = "",
                                              string qq = "", string email = "", bool isOwnAuthor = false,
                                              string memo = "")
                {
                        var model = new ContactModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        DepartmentId = Id,
                                        Name = name,
                                        Title = title,
                                        Duty = duty,
                                        Telephone = telephone,
                                        QQ = qq,
                                        Email = email,
                                        IsOwnAuthor = isOwnAuthor,
                                        Memo = memo
                                };

                        return new Contact(model);
                }

                /// <summary>
                /// 创建专业开设
                /// </summary>
                /// <param name="majorId">专业标识</param>
                /// <param name="managerName">负责人姓名</param>
                /// <param name="teachingManagerName">教学负责人姓名</param>
                /// <returns>
                /// 创建的专业开设对象
                /// </returns>
                public MajorSetup CreateMajorSetup(string majorId, string managerName="", string teachingManagerName="")
                {
                        var model = new MajorSetupModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                DepartmentId = Id,
                                MajorId = majorId,
                                ManagerName = managerName,
                                TeachingManagerName = teachingManagerName
                        };

                        return new MajorSetup(model);
                }

                #endregion


                
        }
}